package cn.qzuie.smart.module.dorm.service.energyequipment;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.qzuie.smart.module.dorm.controller.admin.energyequipment.vo.*;
import cn.qzuie.smart.module.dorm.dal.dataobject.energyequipment.EnergyEquipmentDO;
import cn.qzuie.smart.framework.common.pojo.PageResult;
import cn.qzuie.smart.framework.common.pojo.PageParam;
import cn.qzuie.smart.framework.common.util.object.BeanUtils;

import cn.qzuie.smart.module.dorm.dal.mysql.energyequipment.EnergyEquipmentMapper;

import static cn.qzuie.smart.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.qzuie.smart.framework.common.util.collection.CollectionUtils.convertList;
import static cn.qzuie.smart.framework.common.util.collection.CollectionUtils.diffList;
import static cn.qzuie.smart.module.dorm.enums.ErrorCodeConstants.*;

/**
 * 能源设备 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class EnergyEquipmentServiceImpl implements EnergyEquipmentService {

    @Resource
    private EnergyEquipmentMapper energyEquipmentMapper;

    @Override
    public Long createEnergyEquipment(EnergyEquipmentSaveReqVO createReqVO) {
        // 插入
        EnergyEquipmentDO energyEquipment = BeanUtils.toBean(createReqVO, EnergyEquipmentDO.class);
        energyEquipmentMapper.insert(energyEquipment);

        // 返回
        return energyEquipment.getId();
    }

    @Override
    public void updateEnergyEquipment(EnergyEquipmentSaveReqVO updateReqVO) {
        // 校验存在
        validateEnergyEquipmentExists(updateReqVO.getId());
        // 更新
        EnergyEquipmentDO updateObj = BeanUtils.toBean(updateReqVO, EnergyEquipmentDO.class);
        energyEquipmentMapper.updateById(updateObj);
    }

    @Override
    public void deleteEnergyEquipment(Long id) {
        // 校验存在
        validateEnergyEquipmentExists(id);
        // 删除
        energyEquipmentMapper.deleteById(id);
    }

    @Override
        public void deleteEnergyEquipmentListByIds(List<Long> ids) {
        // 删除
        energyEquipmentMapper.deleteByIds(ids);
        }


    private void validateEnergyEquipmentExists(Long id) {
        if (energyEquipmentMapper.selectById(id) == null) {
            throw exception(ENERGY_EQUIPMENT_NOT_EXISTS);
        }
    }

    @Override
    public EnergyEquipmentDO getEnergyEquipment(Long id) {
        return energyEquipmentMapper.selectById(id);
    }

    @Override
    public PageResult<EnergyEquipmentDO> getEnergyEquipmentPage(EnergyEquipmentPageReqVO pageReqVO) {
        return energyEquipmentMapper.selectPage(pageReqVO);
    }

}